﻿using Newtonsoft.Json.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FM_Key
{
    public FM_KeyType type;
     //[FM_NoteValue]
    public int note;
     //[FM_Accidental]
    public int accidental;
    public int octave;
     //[FM_DurationValue]
    public FM_DurationValue duration;
    public bool stemUp;
    public bool stem;
    public string tie;
    public string beam;
    public string tuple;
    public int stave;          // 0 or 1
    public int voice;          // 0, 1, 2, 3...
    public FM_ClefValue clef;
    public int chord;

    public FM_Key(string jsonString)
    {
        string key = jsonString;

        JArray jsonArray = JArray.Parse(jsonString);

        key = key.Replace(" ", "").Replace("\\", "").Replace("\n", "").Replace("\"", "").Replace("[", "").Replace("]", "").ToLower().Trim();
        if (key.Contains("bar"))
        {
            this.type = FM_KeyType.Bar;
            return;
        }
        string[] s = new string[9];

        for (int i = 0; i < jsonArray.Count; i++)
        {
            var str = jsonArray[i].ToString();
            s[i] = str.Replace("/", "").ToLower().Trim();
        }

        if (key.Contains("bass"))
        {
            this.type = FM_KeyType.Clef;
            this.clef = FM_ClefValue.BASS;
            this.chord = int.Parse(s[1]);
            return;
        }

        if (key.Contains("treble"))
        {
            this.type = FM_KeyType.Clef;
            this.clef = FM_ClefValue.TREBLE;
            this.chord = int.Parse(s[1]);
            return;
        }

        this.type = FM_KeyType.Key;

        string temp = s[0];

        //Debug.LogError($"temp:{temp}");
        bool found = false;

        if (temp.StartsWith("do"))
        {
            this.note = FM_NoteValue.DO;
            temp = temp.Substring(2);
            found = true;
        }

        if (!found && temp.StartsWith("re"))
        {
            this.note = FM_NoteValue.RE;
            temp = temp.Substring(2);
            found = true;
        }

        if (!found && temp.StartsWith("mi"))
        {
            this.note = FM_NoteValue.MI;
            temp = temp.Substring(2);
            found = true;
        }

        if (!found && temp.StartsWith("fa"))
        {
            this.note = FM_NoteValue.FA;
            temp = temp.Substring(2);
            found = true;
        }

        if (!found && temp.StartsWith("sol"))
        {
            this.note = FM_NoteValue.SOL;
            temp = temp.Substring(3);
            found = true;
        }

        if (!found && temp.StartsWith("la"))
        {
            this.note = FM_NoteValue.LA;
            temp = temp.Substring(2);
            found = true;
        }

        if (!found && temp.StartsWith("si"))
        {
            this.note = FM_NoteValue.SI;
            temp = temp.Substring(2);
            found = true;
        }

        if (!found && temp.StartsWith("c"))
        {
            this.note = FM_NoteValue.DO;
            temp = temp.Substring(1);
            found = true;
        }

        if (!found && temp.StartsWith("d"))
        {
            this.note = FM_NoteValue.RE;
            temp = temp.Substring(1);
            found = true;
        }

        if (!found && temp.StartsWith("e"))
        {
            this.note = FM_NoteValue.MI;
            temp = temp.Substring(1);
            found = true;
        }

        if (!found && temp.StartsWith("f"))
        {
            this.note = FM_NoteValue.FA;
            temp = temp.Substring(1);
            found = true;
        }

        if (!found && temp.StartsWith("g"))
        {
            this.note = FM_NoteValue.SOL;
            temp = temp.Substring(1);
            found = true;
        }

        if (!found && temp.StartsWith("a"))
        {
            this.note = FM_NoteValue.LA;
            temp = temp.Substring(1);
            found = true;
        }

        if (!found && temp.StartsWith("b"))
        {
            this.note = FM_NoteValue.SI;
            temp = temp.Substring(1);
            found = true;
        }

        if (!found && temp.StartsWith("r"))
        {
            this.note = FM_NoteValue.REST;
            temp = temp.Substring(1);
        }

        int accidental = 0;
        found = false;

        if (temp.Contains("(")) accidental = FM_Accidental.Courtesy;

        if (temp.Contains("###"))
        {
            accidental = accidental + FM_Accidental.TripleSharp;
            found = true;
        }

        if (!found && temp.Contains("##"))
        {
            accidental = accidental + FM_Accidental.DoubleSharp;
            found = true;
        }

        if (!found && temp.Contains("#"))
        {
            accidental = accidental + FM_Accidental.Sharp;
            found = true;
        }

        if (!found && temp.Contains("bbb"))
        {
            accidental = accidental + FM_Accidental.TripleFlat;
            found = true;
        }

        if (!found && temp.Contains("bb"))
        {
            accidental = accidental + FM_Accidental.DoubleFlat;
            found = true;
        }

        if (!found && temp.Contains("b"))
        {
            accidental = accidental + FM_Accidental.Flat;
            found = true;
        }

        if (!found && temp.Contains("n"))
        {
            accidental = accidental + FM_Accidental.Natural;
        }

        this.accidental = accidental;

        if (string.IsNullOrEmpty(temp)) this.octave = 0;
        else
        {
            //Debug.LogError($"temp2:{ temp}");
            this.octave = temp.Contains("-") ? int.Parse(temp.Substring(temp.Length - 2)) : int.Parse(temp.Substring(temp.Length - 1));
        }

        this.duration = FM_DurationValue.NOTE_WHOLE;

        if (s[1].Equals("wd")) this.duration = FM_DurationValue.NOTE_WHOLE_D;
        if (s[1].Equals("wdr")) this.duration = FM_DurationValue.NOTE_WHOLE_D;
        if (s[1].Equals("h")) this.duration = FM_DurationValue.NOTE_HALF;
        if (s[1].Equals("hr")) this.duration = FM_DurationValue.NOTE_HALF;
        if (s[1].Equals("hd")) this.duration = FM_DurationValue.NOTE_HALF_D;
        if (s[1].Equals("hdr")) this.duration = FM_DurationValue.NOTE_HALF_D;
        if (s[1].Equals("q")) this.duration = FM_DurationValue.NOTE_QUARTER;
        if (s[1].Equals("qr")) this.duration = FM_DurationValue.NOTE_QUARTER;
        if (s[1].Equals("qd")) this.duration = FM_DurationValue.NOTE_QUARTER_D;
        if (s[1].Equals("qdr")) this.duration = FM_DurationValue.NOTE_QUARTER_D;
        if (s[1].Equals("8")) this.duration = FM_DurationValue.NOTE_EIGHTH;
        if (s[1].Equals("8r")) this.duration = FM_DurationValue.NOTE_EIGHTH;
        if (s[1].Equals("8d")) this.duration = FM_DurationValue.NOTE_EIGHTH_D;
        if (s[1].Equals("8dr")) this.duration = FM_DurationValue.NOTE_EIGHTH_D;
        if (s[1].Equals("16")) this.duration = FM_DurationValue.NOTE_SIXTEENTH;
        if (s[1].Equals("16r")) this.duration = FM_DurationValue.NOTE_SIXTEENTH;
        if (s[1].Equals("16d")) this.duration = FM_DurationValue.NOTE_SIXTEENTH_D;
        if (s[1].Equals("16dr")) this.duration = FM_DurationValue.NOTE_SIXTEENTH_D;
        if (s[1].Equals("32")) this.duration = FM_DurationValue.NOTE_THIRTY_SECOND;
        if (s[1].Equals("32r")) this.duration = FM_DurationValue.NOTE_THIRTY_SECOND;
        if (s[1].Equals("32d")) this.duration = FM_DurationValue.NOTE_THIRTY_SECOND_D;
        if (s[1].Equals("32dr")) this.duration = FM_DurationValue.NOTE_THIRTY_SECOND_D;

        this.stemUp = !s[2].Equals("down");
        this.stem = !s[2].Equals("none");
        this.tie = s[3];
        this.beam = s[4];
        this.tuple = s[5];
        this.stave = int.Parse(s[6]);
        this.voice = int.Parse(s[7]);
        this.chord = int.Parse(s[8]);
    }
}
